#include "conf.h"

int *video_quality = &conf.video_quality;
int *video_mode    = &conf.video_mode;

long def_table[24]={0x2000, 0x38D, 0x788, 0x5800, 0x9C5, 0x14B8, 0x10000, 0x1C6A, 0x3C45, 0x8000, 0xE35, 0x1E23,
           0x1CCD, -0x2E1, -0x579, 0x4F33, -0x7EB, -0xF0C, 0xE666, -0x170A, -0x2BC6, 0x7333, -0xB85, -0x15E3};

long table[24];

void change_video_tables(int a, int b){
 int i;
 for (i=0;i<24;i++) table[i]=(def_table[i]*a)/b; 
}

long CompressionRateTable[]={0x60, 0x5D, 0x5A, 0x57, 0x54, 0x51, 0x4D, 0x48, 0x42, 0x3B, 0x32, 0x29, 0x22, 0x1D, 0x17, 0x14, 0x10, 0xE, 0xB, 9, 7, 6, 5, 4, 3, 2, 1};




void __attribute__((naked,noinline)) movie_record_task(){ 
 asm volatile(
                 "STMFD   SP!, {R2-R8,LR}\n"
                 "LDR     R8, =0x648\n"
                 "LDR     R7, =0x2710\n"
                 "LDR     R4, =0x56C8\n"
                 "MOV     R6, #0\n"
                 "MOV     R5, #1\n"
 "loc_FFD0C994:\n"
                 "LDR     R0, [R4,#0x18]\n"
                 "MOV     R2, #0\n"
                 "ADD     R1, SP, #4\n"
                 "BL      sub_FFC28AA0\n"
                 "LDR     R0, [R4,#0x20]\n"
                 "CMP     R0, #0\n"
                 "BNE     loc_FFD0CA64\n"
                 "LDR     R0, [SP,#4]\n"
                 "LDR     R1, [R0]\n"
                 "SUB     R1, R1, #2\n"
                 "CMP     R1, #9\n"
                 "ADDLS   PC, PC, R1,LSL#2\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0C9C8:\n"
                 "B       loc_FFD0CA18\n"
 "loc_FFD0C9CC:\n"
                 "B       loc_FFD0CA38\n"
 "loc_FFD0C9D0:\n"
                 "B       loc_FFD0CA48\n"
 "loc_FFD0C9D4:\n"
                 "B       loc_FFD0CA50\n"
 "loc_FFD0C9D8:\n"
                 "B       loc_FFD0CA20\n"
 "loc_FFD0C9DC:\n"
                 "B       loc_FFD0CA58\n"
 "loc_FFD0C9E0:\n"
                 "B       loc_FFD0CA28\n"
 "loc_FFD0C9E4:\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0C9E8:\n"
                 "B       loc_FFD0CA60\n"
 "loc_FFD0C9EC:\n"
                 "B       loc_FFD0C9F0\n"
 "loc_FFD0C9F0:\n"
                 "LDR     R0, =0xFFD0C688\n"
                 "STR     R6, [R4,#0x34]\n"
                 "STR     R0, [R4,#0xA8]\n"
                 "LDR     R0, =0xFFD0C118\n"
                 "LDR     R2, =0xFFD0C034\n"
                 "LDR     R1, =0x6EB90\n"
                 "STR     R6, [R4,#0x24]\n"
                 "BL      sub_FFCB6618\n"
                 "STR     R5, [R4,#0x38]\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA18:\n"
                 "BL      unlock_optical_zoom\n"   //  +
                 "BL      sub_FFD0C780\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA20:\n"
                 "BL      sub_FFD0C488_my\n"       //----------->
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA28:\n"
                 "LDR     R1, [R0,#0x10]\n"
                 "LDR     R0, [R0,#4]\n"
                 "BL      sub_FFDF8CF0\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA38:\n"
                 "LDR     R0, [R4,#0x38]\n"
                 "CMP     R0, #5\n"
                 "STRNE   R5, [R4,#0x28]\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA48:\n"
                 "BL      sub_FFD0C2A0\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA50:\n"
                 "BL      sub_FFD0C164\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA58:\n"
                 "BL      sub_FFD0BFC0\n"
                 "B       loc_FFD0CA64\n"
 "loc_FFD0CA60:\n"
                 "BL      sub_FFD0CBCC\n"
 "loc_FFD0CA64:\n"
                 "LDR     R1, [SP,#4]\n"
                 "LDR     R3, =0xFFD0BE50\n"
                 "STR     R6, [R1]\n"
                 "STR     R8, [SP]\n"
                 "LDR     R0, [R4,#0x1C]\n"
                 "MOV     R2, R7\n"
                 "BL      sub_FFC0F5C0\n"
                 "B       loc_FFD0C994\n"
 );
}


void __attribute__((naked,noinline)) sub_FFD0C488_my(){ 
 asm volatile(
                 "STMFD   SP!, {R4-R8,LR}\n"
                 "SUB     SP, SP, #0x40\n"
                 "MOV     R6, #0\n"
                 "LDR     R5, =0x5468\n"
                 "MOV     R4, R0\n"
                 "STR     R6, [SP,#0x30]\n"
                 "STR     R6, [SP,#0x28]\n"
                 "LDR     R0, [R5,#0x38]\n"
                 "MOV     R8, #4\n"
                 "CMP     R0, #3\n"
                 "STREQ   R8, [R5,#0x38]\n"
                 "LDR     R0, [R5,#0xA8]\n"
                 "BLX     R0\n"
                 "LDR     R0, [R5,#0x38]\n"
                 "CMP     R0, #4\n"
                 "BNE     loc_FFD0C560\n"
                 "ADD     R3, SP, #0x28\n"
                 "ADD     R2, SP, #0x2C\n"
                 "ADD     R1, SP, #0x30\n"
                 "ADD     R0, SP, #0x34\n"
                 "BL      sub_FFDF8E84\n"
                 "CMP     R0, #0\n"
                 "MOV     R7, #1\n"
                 "BNE     loc_FFD0C504\n"
                 "LDR     R1, [R5,#0x28]\n"
                 "CMP     R1, #1\n"
                 "BNE     loc_FFD0C568\n"
                 "LDR     R1, [R5,#0x50]\n"
                 "LDR     R2, [R5,#0x3C]\n"
                 "CMP     R1, R2\n"
                 "BCC     loc_FFD0C568\n"
 "loc_FFD0C504:\n"
                 "CMP     R0, #0x80000001\n"
                 "STREQ   R8, [R5,#0x64]\n"
                 "BEQ     loc_FFCE528C\n"
                 "CMP     R0, #0x80000003\n"
                 "STREQ   R7, [R5,#0x64]\n"
                 "BEQ     loc_FFCE528C\n"
                 "CMP     R0, #0x80000005\n"
                 "MOVEQ   R0, #2\n"
                 "BEQ     loc_FFD0C538\n"
                 "CMP     R0, #0x80000007\n"
                 "STRNE   R6, [R5,#0x64]\n"
                 "BNE     loc_FFCE528C\n"
                 "MOV     R0, #3\n"
 "loc_FFD0C538:\n"
                 "STR     R0, [R5,#0x64]\n"
 "loc_FFCE528C:\n"
                 "LDR     R0, =0x6EBC0\n"
                 "LDR     R0, [R0,#8]\n"
                 "CMP     R0, #0\n"
                 "BEQ     loc_FFD0C554\n"
                 "BL      sub_FFC445C4\n"
                 "B       loc_FFD0C558\n"
 "loc_FFD0C554:\n"
                 "BL      sub_FFD0BFC0\n"
 "loc_FFD0C558:\n"
                 "MOV     R0, #5\n"
                 "STR     R0, [R5,#0x38]\n"
 "loc_FFD0C560:\n"
                 "ADD     SP, SP, #0x40\n"
                 "LDMFD   SP!, {R4-R8,PC}\n"
 "loc_FFD0C568:\n"
                 "LDR     LR, [SP,#0x30]\n"
                 "CMP     LR, #0\n"
                 "BEQ     loc_FFD0C630\n"
                 "STR     R7, [R5,#0x2C]\n"
                 "LDR     R0, [R5,#0x7C]\n"
                 "LDR     R1, [R4,#0x14]\n"
                 "LDR     R2, [R4,#0x18]\n"
                 "LDR     R12, [R4,#0xC]\n"
                 "ADD     R3, SP, #0x38\n"
                 "ADD     R8, SP, #0x14\n"
                 "STMIA   R8, {R0-R3}\n"
                 "LDR     R3, [R5,#0x68]\n"
                 "ADD     R2, SP, #0x3C\n"
                 "ADD     R8, SP, #8\n"
                 "LDRD    R0, [SP,#0x28]\n"
                 "STMIA   R8, {R0,R2,R3}\n"
                 "STR     R1, [SP,#4]\n"
                 "STR     LR, [SP]\n"
                 "LDMIB   R4, {R0,R1}\n"
                 "LDR     R3, [SP,#0x34]\n"
                 "MOV     R2, R12\n"
                 "BL      sub_FFDCE504\n"
                 "LDR     R0, [R5,#0x10]\n"
                 "LDR     R1, [R5,#0x58]\n"
                 "BL      sub_FFC0F034\n"
                 "CMP     R0, #9\n"
                 "BNE     loc_FFD0C5E4\n"
                 "BL      sub_FFDF9470\n"
                 "MOV     R0, #0x90000\n"
                 "STR     R7, [R5,#0x38]\n"
                 "B       loc_FFD0C5FC\n"
 "loc_FFD0C5E4:\n"
                 "LDR     R0, [SP,#0x38]\n"
                 "CMP     R0, #0\n"
                 "BEQ     loc_FFD0C604\n"
                 "BL      sub_FFDF9470\n"
                 "MOV     R0, #0xA0000\n"
                 "STR     R7, [R5,#0x38]\n"
 "loc_FFD0C5FC:\n"
                 "BL      sub_FFC6D0E0\n"
                 "B       loc_FFD0C560\n"
 "loc_FFD0C604:\n"
                 "BL      sub_FFDCE5C8\n"
                 "LDR     R0, [SP,#0x34]\n"
                 "LDR     R1, [SP,#0x3C]\n"
                 "BL      sub_FFDF9218\n"
                 "LDR     R0, [R5,#0x5C]\n"
                 "LDR     R1, =0x54E4\n"
                 "ADD     R0, R0, #1\n"
                 "STR     R0, [R5,#0x5C]\n"
                 "LDR     R0, [SP,#0x3C]\n"
                 "MOV     R2, #0\n"
                 "BL      sub_FFDF703C_my\n"  //---------->
 "loc_FFD0C630:\n"
                 "LDR     R0, [R5,#0x60]\n"
                 "ADD     R0, R0, #1\n"
                 "STR     R0, [R5,#0x60]\n"
                 "LDR     R1, [R5,#0x4C]\n"
                 "MUL     R0, R1, R0\n"
                 "LDR     R1, [R5,#0x48]\n"
                 "BL      sub_FFE8291C\n"
                 "MOV     R4, R0\n"
                 "BL      sub_FFDF94A8\n"
                 "LDR     R1, [R5,#0x80]\n"
                 "CMP     R1, R4\n"
                 "BNE     loc_FFD0C66C\n"
                 "LDR     R0, [R5,#0x30]\n"
                 "CMP     R0, #1\n"
                 "BNE     loc_FFD0C680\n"
 "loc_FFD0C66C:\n"
                 "LDR     R1, [R5,#0x8C]\n"
                 "MOV     R0, R4\n"
                 "BLX     R1\n"
                 "STR     R4, [R5,#0x80]\n"
                 "STR     R6, [R5,#0x30]\n"
 "loc_FFD0C680:\n"
                 "STR     R6, [R5,#0x2C]\n"
                 "B       loc_FFD0C560\n"
 );
}

void __attribute__((naked,noinline)) sub_FFDF703C_my(){ 
 asm volatile(
                 "STMFD   SP!, {R4-R8,LR}\n"
                 "LDR     R4, =0x8940\n"
                 "LDR     LR, [R4]\n"
                 "LDR     R2, [R4,#8]\n"
                 "CMP     LR, #0\n"
                 "LDRNE   R3, [R4,#0xC]\n"
                 "MOV     R5, R2\n"
                 "CMPNE   R3, #1\n"
                 "MOVEQ   R2, #0\n"
                 "STREQ   R0, [R4]\n"
                 "STREQ   R2, [R4,#0xC]\n"
                 "BEQ     loc_FFDF7108\n"
                 "LDR     R3, [R4,#4]\n"
                 "LDR     R7, =table\n"            // *
                 "ADD     R12, R3, R3,LSL#1\n"
                 "LDR     R3, [R7,R12,LSL#2]\n"
                 "ADD     R6, R7, #0x30\n"
                 "LDR     R8, [R6,R12,LSL#2]\n"
                 "SUB     R3, LR, R3\n"
                 "CMP     R3, #0\n"
                 "SUB     LR, LR, R8\n"
                 "BLE     loc_FFDF70C4\n"
                 "ADD     R12, R7, R12,LSL#2\n"
                 "LDR     LR, [R12,#4]\n"
                 "CMP     LR, R3\n"
                 "ADDGE   R2, R2, #1\n"
                 "BGE     loc_FFDF70B8\n"
                 "LDR     R12, [R12,#8]\n"
                 "CMP     R12, R3\n"
                 "ADDLT   R2, R2, #3\n"
                 "ADDGE   R2, R2, #2\n"
 "loc_FFDF70B8:\n"
           //    "CMP     R2, #0x17\n"   // -
           //    "MOVGE   R2, #0x16\n"   // -
                 "CMP     R2, #0x1A\n"   // +
                 "MOVGE   R2, #0x19\n"   // +

                 "B       loc_FFDF70F8\n"
 "loc_FFDF70C4:\n"
                 "CMP     LR, #0\n"
                 "BGE     loc_FFDF70F8\n"
                 "ADD     R3, R6, R12,LSL#2\n"
                 "LDR     R12, [R3,#4]\n"
                 "CMP     R12, LR\n"
                 "SUBLE   R2, R2, #1\n"
                 "BLE     loc_FFDF70F0\n"
                 "LDR     R3, [R3,#8]\n"
                 "CMP     R3, LR\n"
                 "SUBGT   R2, R2, #3\n"
                 "SUBLE   R2, R2, #2\n"
 "loc_FFDF70F0:\n"
                 "CMP     R2, #0\n"
                 "MOVLT   R2, #0\n"
 "loc_FFDF70F8:\n"
                 "CMP     R2, R5\n"
                 "STRNE   R2, [R4,#8]\n"
                 "MOVNE   R2, #1\n"
                 "STRNE   R2, [R4,#0xC]\n"
 "loc_FFDF7108:\n"
                 "LDR     R2, =CompressionRateTable\n"  // *
                 "LDR     R3, [R4,#8]\n"
                 "LDR     R2, [R2,R3,LSL#2]\n"

                 "LDR     R3, =video_mode\n"      // +
                 "LDR     R3, [R3]\n"             // +
                 "LDR     R3, [R3]\n"             // +
                 "CMP     R3, #1\n"               // +
                 "LDREQ   R3, =video_quality\n"   // +     
                 "LDREQ   R3, [R3]\n"             // +     
                 "LDREQ   R2, [R3]\n"             // +     

                 "STR     R2, [R1]\n"
                 "STR     R0, [R4]\n"
                 "BL      mute_on_zoom\n"         // +
                 "LDMFD   SP!, {R4-R8,PC}\n"
 );
}

